﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class EditorAssetBundle : IAssetBundle
{

    private string _bundleName = null;
    private AssetBundle _ab = null;
    private int refCount = 0;
    private List<IAssetBundle> _refABList = null;
    private Action _callback = null;


    private EditorAssetLoader _abLoader;

    public EditorAssetBundle(EditorAssetLoader abLoader)
    {
        _abLoader = abLoader;
    }

    public void AddReferenceCount()
    {
        ++refCount;
    }

    public void CreateRes(string bundleName)
    {
        //throw new NotImplementedException();
        string[] strs = AssetDatabase.GetAssetBundleDependencies(bundleName, true);
        if(strs == null ||  strs.Length == 0)
        {
            //不需要加载依赖文件
            _abLoader.AddAssetBundle(_bundleName, this);
        }
        else
        {
            //需要加载依赖文件
            for (int i = 0; i < strs.Length; i++)
            {
                EditorAssetBundle tempIab = _abLoader.GetIAssetBundle(strs[i]) as EditorAssetBundle;
                if(tempIab != null)
                {
                    EditorAssetBundle eab = new EditorAssetBundle(_abLoader);
                    eab.CreateRes(strs[i]);
                    if (_refABList == null)
                    {
                        _refABList = new List<IAssetBundle>();
                    }
                    _refABList.Add(eab);
                }
                else
                {
                    tempIab.AddReferenceCount();
                }
            }
        }
        if (_callback != null)
        {
            _callback();
        }
    }

    public void CreateRes(string bundleName, Action callback)
    {
        _bundleName = bundleName;
        _callback = callback;
        CreateRes(bundleName);
    }

    public string GetABName()
    {
        return _bundleName;
    }

    public AssetBundle GetAssetBundle()
    {
        return null;
    }

    public bool IsDone()
    {
        throw new NotImplementedException();
    }

    public T LoadRes<T>(string assetName) where T : UnityEngine.Object
    {
        string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(_bundleName, assetName);
        T t = AssetDatabase.LoadAssetAtPath<T>(assetPaths[0]);
        if (t == null)
        {
            Debug.LogError("!!资源加载失败：" + assetName);
        }
        else
        {
            return t;
        }
        return default(T);
    }

    public int ReferenceCount()
    {
        return refCount;
    }

    public void ReleaseRes()
    {
        if (_refABList != null)
        {
            for (int i = 0; i < _refABList.Count; i++)
            {
                _refABList[i].ReleaseRes();
            }
        }
        SubReferenceCount();
    }

    public void SubReferenceCount()
    {
        --refCount;
        if(refCount <= 0)
        {
            UnLoadAB();
        }
    }

    public void UnLoadAB()
    {
        refCount = 0;
        _refABList = null;
        _abLoader = null;
        Debug.Log("======unload:" + _bundleName);
        _ab = null;
        LoadManager.Instance.UnLoadAB(_bundleName);
        _bundleName = null;
    }
}
